package com.wangwenjun.concurrency.chapter14.future;

import java.util.concurrent.*;

/**
 * @Package: com.wangwenjun.concurrency.chapter14.future
 * @ClassName: UseFuture1
 * @Author: lujieni
 * @Description: 1
 * @Date: 2021-03-16 10:33
 * @Version: 1.0
 */
public class UseFuture1 {

    public static void main(String[] args)  {
        testException();
        //testCancelFail();
        //testCancelSuccess();
    }


    public static void testException(){
        ExecutorService service = Executors.newSingleThreadExecutor();//默认无界
        Future<Integer> future = service.submit(() -> {
            //int i = 5/0; //人为运行时异常
            throw new RuntimeException("aaaa");
            //return 1;
        });
        try {
            Integer result = future.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            System.out.println("Exception"); // int i = 5/0 这里会catch submit中的异常
            e.printStackTrace();
        }
        System.out.println("finish");
    }

    /**
     * Future如果想取消线程池正在执行的任务,那么就必须在task中判断中断flag
     */
    public static void testCancelFail(){
        ExecutorService service = Executors.newSingleThreadExecutor();//默认无界
        Future<Integer> future = service.submit(() -> {
            while (true){
                System.out.println("working");
            }
        });
        try {
            TimeUnit.MILLISECONDS.sleep(20);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        future.cancel(true);
        System.out.println("finish");
    }

    public static void testCancelSuccess(){
        ExecutorService service = Executors.newSingleThreadExecutor(); //默认无界
        Future<Integer> future = service.submit(() -> {
            while (!Thread.currentThread().isInterrupted()){
                System.out.println("working");
            }
            System.out.println("a:"+Thread.currentThread().isInterrupted()); // true
            return 10;
        });
        try {
            TimeUnit.MILLISECONDS.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        future.cancel(true);


        Future<Integer> submit = service.submit(() -> {
            System.out.println("b:"+Thread.currentThread().isInterrupted()); // false 由线程池重置打断标记为false
            return 100;
        });

        try {
            Integer i = submit.get();
            System.out.println(i);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }



      /*  try {
            Integer i = future.get(); // future.cancel过后再get是无效的
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }*/
        System.out.println("finish");
    }
}